package com.thingsgrid.mqtthelper.controller;

import com.thingsgrid.mqtthelper.feign.MqttHelper;
import com.thingsgrid.mqtthelper.service.MqttHelperService;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 功能说明：mqtt消息处理
 *
 * @author 李龙飞
 * @since 2019/8/27 9:06
 */

@RestController
@RequestMapping("mqtt/api")
public class MqttHelperController implements MqttHelper {

	@Autowired
	private MqttHelperService mqttHelperService;


	/**
	 *说明：新增topic
	 *@author lilongfei
	 *@since 2019/8/27 10:01
	 */
	@Override
	@GetMapping("/addTopic")
	public R addTopic(String[] topicList) {
		boolean addResult = mqttHelperService.addMqttTopic(topicList);
		if(addResult){
			return R.success("新增成功");
		}else{
			return R.success("新增失败");
		}
	}

	/**
	 *说明：删除topic
	 *@author lilongfei
	 *@since 2019/8/27 10:01
	 */
	@Override
	@GetMapping("/removeTopic")
	public R removeTopic(@RequestParam("topic") String topic) {
		boolean removeResult = mqttHelperService.removeMqttTopic(topic);
		if(removeResult){
			return R.success("删除成功");
		}else{
			return R.success("删除失败");
		}
	}

	@Override
	@PostMapping("/sendMsg")
	public R sendMsg(String topic, String msg) {
		System.out.println(topic+"/"+msg);
		Object object = mqttHelperService.sendMsg(topic, msg);
		return R.data(object);
	}

}
